package com.fengcheng.controller;

import com.fengcheng.service.FileService;
import com.fengcheng.util.JWTUtil;
import com.fengcheng.vo.ReturnVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/user/file")
public class FileController {
    @Autowired
    FileService fileService;

    /*
     * @author 枫城
     * @createTime 2022/7/8 17:51
     * @desc token解析
     */
    public String getName(
            HttpServletRequest request
    ){
        String token = request.getHeader("token");
        return JWTUtil.getValue(token,"userName");
    }


    /*
     * @author 枫城
     * @createTime 2022/7/10 11:10
     * @desc 查看文件列表
     */
    @ResponseBody
    @PostMapping("getList")
    public List<Map<String, Object>> getFileList(
            HttpServletRequest request,
            @RequestParam("srcPath") String srcPath
    ) throws Exception {
        return fileService.getFileList(request,srcPath);
    }

    /*
     * @author 枫城
     * @createTime 2022/7/6 9:10
     * @desc 创建空文件夹
     */
    @PostMapping("/mkdir")
    @ResponseBody
    public ReturnVo mkdir(
            @RequestParam("path") String path,
            HttpServletRequest request
    ) throws Exception {

        return fileService.mkdir(path,request);
    }

    /*
     * @author 枫城
     * @createTime 2022/7/6 9:10
     * @desc 上传文件 (同一文件名，相同位置重复上传未解决)
     */
    @ResponseBody
    @PostMapping("/uploadFile")
    public ReturnVo uploadFile(
            @RequestParam("file") MultipartFile[] fileList,
            @RequestParam("srcPath") String srcPath,
            HttpServletRequest request
    ) throws Exception {
        return fileService.uploadFile(fileList, srcPath, request);
    }


    /**
     * 文件下载
     * @param srcUri
     * @return
     */
    @ResponseBody
    @GetMapping ("/upDownFile")
    public ReturnVo upDownFile(
            @RequestBody Map<String,String> srcUri,
            HttpServletRequest request)
    {
        return fileService.upDownFile(srcUri, request);

    }

    /**
     * 删除文件
     * @param data
     * @param request
     * @return
     * @throws Exception
     */

    @ResponseBody
    @GetMapping("deleteFile")
    public Boolean deleteFile(@RequestBody Map<String, String> data,
                                          HttpServletRequest request) throws Exception {
        String srcPath = data.get("srcPath");
        String fileName = data.get("fileName");
        String path = srcPath+fileName;
        return fileService.deleteFile(path, request);
    }

    /**
     * 重命名文件
     * @param filePath
     * @param newFileName
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/renameFile")
    public Map<String, String> FixFileName(@RequestParam("filePath") String filePath, @RequestParam("newFileName") String newFileName,HttpServletRequest request) {

        Map<String, String> resultMap = new HashMap<>();
        try {
            fileService.reName(filePath, newFileName, request);
            resultMap.put("state", "ok");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("state", "no");
            return resultMap;
        }

    }

    /**
     * 移动文件
     * @param oldPath
     * @param newPath
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/moveFile")
    public Boolean moveFile(@RequestParam("oldPath") String oldPath,@RequestParam("newPath") String newPath, HttpServletRequest request) {

        try {
            fileService.moveFile(oldPath, newPath, request);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }


}


